using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Jamila2.CoreElements;
using Jamila2.Database;
using Jamila2.Tools;

namespace BPCSFileOutputDaemon
{
    public class Program
    {
        static void Main(string[] args)
        {
            StringBuilder LogBuilder = new StringBuilder();
            int StepsCounter = 0;

            DaemonConfigurator CurConfigurator = new DaemonConfigurator();

            if (CurConfigurator.ReadConfiguration())
            {
                
                LogBuilder.AppendLine((StepsCounter++) + ". Daemon start watching.");

                // Generate normal file                
                FileGenerator CurGenerator = new FileGenerator(CurConfigurator.PathForOutputFiles, CurConfigurator.PathForArchievedFiles, CurConfigurator.DatabaseConnection, CurConfigurator.DatabaseType, CurConfigurator.Email_Active, CurConfigurator.SMTP);

                CurGenerator.GenerateClaimTransactionFile(TypeOfFile.ColumnLengthOriented);
                
                    for (int x = 0; x < CurGenerator.LoggedMessages.Count; x++)
                    {
                        LogBuilder.AppendLine((StepsCounter++) + ". " + CurGenerator.LoggedMessages[x].ToString());
                    }

                    LogBuilder.AppendLine((StepsCounter++) + ". Claim File End");
                    
                

                CurGenerator.GenerateCreditCardTransactionFile(TypeOfFile.ColumnLengthOriented);
                
                    for (int x = 0; x < CurGenerator.LoggedMessages.Count; x++)
                    {
                        LogBuilder.AppendLine((StepsCounter++) + ". " + CurGenerator.LoggedMessages[x].ToString());
                    }
                    LogBuilder.AppendLine((StepsCounter++) + ". Credit Card File End");

                    ////Added By Agnes[31032009] - For enhancement [send out email to claimant and their superior]

                    //CurGenerator.GenerateCreditCardReminder();

                    //for (int x = 0; x < CurGenerator.LoggedMessages.Count; x++)
                    //{
                    //    LogBuilder.AppendLine((StepsCounter++) + ". " + CurGenerator.LoggedMessages[x].ToString());
                    //}
                    //LogBuilder.AppendLine((StepsCounter++) + ". Credit Card Reminder End");

                    //CurGenerator.GenerateClaimReminder();

                    //for (int x = 0; x < CurGenerator.LoggedMessages.Count; x++)
                    //{
                    //    LogBuilder.AppendLine((StepsCounter++) + ". " + CurGenerator.LoggedMessages[x].ToString());
                    //}
                    //LogBuilder.AppendLine((StepsCounter++) + ". Claim Reminder End");


                    CurGenerator = null;

            }
            else
            {
                LogBuilder.AppendLine((StepsCounter++) + ". Error reading configuration : " + CurConfigurator.ErrorMessage);
            }

            LogBuilder.AppendLine((StepsCounter++) + ". Exit. Good Bye.");

            /* The End */
            string NewFolderName = DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year;
            string NewFileName = "log_" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + ".txt";

            if (!Directory.Exists(CurConfigurator.PathForLogFiles + "\\" + NewFolderName))
                Directory.CreateDirectory(CurConfigurator.PathForLogFiles + "\\" + NewFolderName);

            SwissArmy.FileCreate(CurConfigurator.PathForLogFiles + "\\" + NewFolderName + "\\" + NewFileName, LogBuilder.ToString());

            Console.WriteLine(LogBuilder.ToString());

            // clean up
            LogBuilder = null;
            CurConfigurator = null;
        }
    }
}
